package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.Nuyoah
 * @Date2025/5/27 15:21
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShoppingCartDTO dto) {
        //1.获取购物车的基本信息
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());//只能查询自己的购物车信息

        //2.判断购物车内是否存在当前商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //2.1.如果存在则把数量加1
        if (shoppingCartList!=null&&shoppingCartList.size()==1){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);//数量加1
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //3.如果不是则插入数据,判断添加到购物车的是菜品还是套餐
            Long dishId = dto.getDishId();
            if (dishId!=null){
                //添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());//设置菜品名称
                shoppingCart.setAmount(dish.getPrice());//设置商品金额
                shoppingCart.setImage(dish.getImage());//设置商品图片路径
            }else {
                //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list = shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
        return list;
    }

    /**
     * 删除购物车商品数量
     *
     * @param id
     */
    @Override
    public void sub(Long id) {
        shoppingCartMapper.sub(id);
    }

    /**
     * 清空购物车商品
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
